package com.education.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.education.realtime.app.BaseAppV1;
import com.education.realtime.bean.TestExam;
import com.education.realtime.common.Constant;
import com.education.realtime.util.AtguiguUtil;
import com.education.realtime.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

public class Dws_Exam_Stat extends BaseAppV1 {
    public static void main(String[] args) {
        new Dws_Exam_Stat().init(3007, "Dws_Exam_Stat", Constant.TOPIC_DWD_EXAM);
    }


    @Override
    protected void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        stream.map(JSON::parseObject)
                .assignTimestampsAndWatermarks(WatermarkStrategy.<JSONObject>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                        .withTimestampAssigner((obj, ts) -> obj.getLong("ts") * 1000))
                .keyBy(obj -> obj.getString("paper_id"))
                .process(new KeyedProcessFunction<String, JSONObject, TestExam>() {
                    @Override
                    public void processElement(JSONObject obj, Context context, Collector<TestExam> collector) throws Exception {
                        Long ts = obj.getLong("ts");
                        String paper_id = obj.getString("paper_id");
                        Long user_num = 1L;
                        Double score_num = obj.getDouble("score");
                        Long duration_ts = obj.getLong("duration_sec");
                        collector.collect(new TestExam("", "", paper_id, user_num, score_num, duration_ts, ts));
                    }
                })
                .windowAll(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<TestExam>() {
                    @Override
                    public TestExam reduce(TestExam t2, TestExam t1) throws Exception {
                        t1.setUser_num(t2.getUser_num() + t1.getUser_num());
                        t1.setDuration_ts(t1.getDuration_ts() + t2.getDuration_ts());
                        t1.setScore_num(t1.getScore_num() + t2.getScore_num());
                        return t1;
                    }
                }, new ProcessAllWindowFunction<TestExam, TestExam, TimeWindow>() {
                    @Override
                    public void process(Context context, Iterable<TestExam> iterable,
                                        Collector<TestExam> collector) throws Exception {
                        TestExam bean = iterable.iterator().next();
                        String stt = AtguiguUtil.toDateTime(context.window().getStart());
                        String edt = AtguiguUtil.toDateTime(context.window().getEnd());
                        bean.setStt(stt);
                        bean.setEdt(edt);
                        collector.collect(bean);

                    }
                })
              .addSink(FlinkSinkUtil.getClickHouseSink(Constant.CLICKHOUSE_DB,"dws_exam_stat_window",TestExam.class));
                


    }
}